package miejuli;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.net.URL;
import java.util.List;

/**
 * Created by Administrator on 2017/5/26.
 */
public class BuyShopGoods {

    private AppiumDriver driver;
    private String[] phones=new String[]{
            "15817978073",
            "17032039247",
            "13094717161",
            "17080847552",
            "15014875738",
            "15918923309",
            "13124966204",
            "14576598995",
            "13929680557",
            "17095936349",
            "17081144034",
            "13124970974",
            "17098324719",
            "17070145096",
            "15818286349",
            "17190710416",
            "13712514374",
            "15218978015",
            "18429003799"
    };

    public void setUp() throws Exception {

        // set up appium

        File classpathRoot = new File(System.getProperty("user.dir"));

        File appDir = new File(classpathRoot, "/apps");

        File app = new File(appDir, "meijuli.apk");

        DesiredCapabilities capabilities = new DesiredCapabilities();

        capabilities.setCapability("deviceName","MEIZU-M2-88MFBMG3CPZR");

        capabilities.setCapability("platformVersion", "5.1");

        capabilities.setCapability("app", app.getAbsolutePath());

        capabilities.setCapability("appPackage", "com.join51.meijuli");

        capabilities.setCapability("appActivity", "com.join51.meijuli.ui.ActivityMain");
        capabilities.setCapability("noReset", true);//必须打开，打开签名错误

        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

        System.out.println("App is launched!");

    }

    public void sendImage() throws InterruptedException {
        for(int i=0;i<phones.length;i++){
            Thread.sleep(2000);
            driver.findElement(By.id("com.join51.meijuli:id/mine_TV")).click();;//点击我的登陆
            try {
                driver.findElement(By.id("android:id/button1")).click();//确认登陆
            }catch (Exception e){
                driver.findElement(By.id("com.join51.meijuli:id/setting_icon")).click();//点击齿轮
                driver.findElement(By.id("com.join51.meijuli:id/logoutView")).click();//退出
            }finally {
                login(phones[i]);
                driver.findElement(By.id("com.join51.meijuli:id/store_TV")).click();;//点击实体店
            }

        }
    }

    public void login(String phone){
        driver.findElement(By.id("com.join51.meijuli:id/cancelView")).click();
        List<WebElement> els= driver.findElements(By.id("com.join51.meijuli:id/inputEditText"));//输入账号
        for(WebElement el:els){
            if("请输入手机号码".equals(el.getAttribute("text"))){
                el.sendKeys(phone);//输入手机号
            }else{
                el.sendKeys("luwei666");//输入密码
            }
        }
        driver.findElement(By.id("com.join51.meijuli:id/nextStepBtn")).click();//点击登陆按钮
    }

    public void sendSFZ(String phone){
        List<WebElement> els= driver.findElements(By.id("com.join51.meijuli:id/inputEditText"));//输入账号
        for(WebElement el:els){
            if("请输入手机号码".equals(el.getAttribute("text"))){
                el.sendKeys(phone);//输入手机号
            }else{
                el.sendKeys("luwei666");//输入密码
            }
        }
        driver.findElement(By.id("com.join51.meijuli:id/nextStepBtn")).click();//点击登陆按钮
        driver.findElement(By.id("com.join51.meijuli:id/mine_TV")).click();;//点击我的
    }

    public void tearDown() throws Exception {

        driver.quit();

    }

}
